<?php

class DatHangController {

    public function invoke() {
        require_once LIBRARIES_ENTITIES_DATABASE;
        require_once LIBRARIES_ENTITIES_DATHANG;
        require_once LIBRARIES_ENTITIES_HANG_HOA;
        $action = '';
        $view = '';
        if (isset($_GET['view'])) {
            $view = $_GET['view'];
            if ($view == 'view') {
                if (isset($_POST['action'])) {
                    $action = $_POST['action'];
                    if ($action == 'view') {
                        $this->view();
                    } else if ($action == 'add') {
                        $kq = $this->dathang(
                                $_POST['ngaynhan'], $_POST['soluong'], $_POST['loaihs'], $_POST['hoten'], $_POST['mssv'], $_POST['lop'], $_POST['ngaysinh'], $_POST['diachi'], $_POST['sdt'], $_POST['email'], $_POST['ghichu'], $_SESSION['userid'], '',$_POST['tencty'],$_POST['diachicty'],$_POST['sdtcty']);
                        echo $kq;
                    } else if ($action == 'updatestatus') {
                        $mangid = '';
                        if (isset($_POST['mangid'])) {
                            $mangid = explode(',', $_POST['mangid']);
                            foreach ($mangid as $value) {
                                $kq = $this->updatestatus($value, $_POST['trangthai']);
                            }
                            echo $kq;
                        }
                    } else if ($action == 'delete') {
                        $mangid = '';
                        if (isset($_POST['mangid'])) {
                            $mangid = explode(',', $_POST['mangid']);
                            foreach ($mangid as $value) {
                                $kq = $this->delete($value);
                            }
                            echo $kq;
                        }
                    } else if ($action == 'showanh') {
                        echo $this->showanh($_POST['idhoso']);
                    } else if ($action == 'update') {
                        $this->update(4, $_POST['ngaynhan'], $_POST['soluong'], $_POST['loaihoso'], $_POST['hoten'], $_POST['mssv'], $_POST['lop'], $_POST['ngaysinh'], $_POST['diachi'], $_POST['sdt'], $_POST['email'], $_POST['ghichu']);
                    }
                } else {
                    $this->view();
                }
            } else if ($view == 'update') {
                if (isset($_GET['id'])) {
                    $this->loadEdit($_GET['id']);
                }
            } else {
                $this->loadAdd();
            }
        } else {
            $this->view();
        }
    }

    private function view() {

        $dir = dirname(__FILE__);
        $xtpl = new XTemplate($dir . DS . "view" . DS . "index.php");
        $xtpl->assign('INDEX', INDEX);

        $dsdathang = $this->loadList();
        if ($dsdathang != null) {
            foreach ($dsdathang as $value) {
                $xtpl->assign('ID_DATHANG', $value->getDH_ID());
                $xtpl->assign('NGAY_DAT', $value->getDH_NGAYDAT());
                $xtpl->assign('GHI_CHU', $value->getDH_GHICHU());
                $xtpl->assign('LOP', $value->getDH_LOP());
                $xtpl->assign('NGAY_NHAN', $value->getDH_NGAYNHAN());
                $xtpl->assign('TEN_KH', $value->getDH_HOTEN());
                $xtpl->assign('NGAY_SINH', $value->getDH_NGAYSINH());
                $xtpl->assign('MSSV', $value->getDH_MSSV());
                $xtpl->assign('SDT', $value->getDH_SDT());
                $xtpl->assign('DIA_CHI', $value->getDH_DIACHI());
                $xtpl->assign('TEN_HOSO', $value->getHH_TEN());
                $xtpl->assign('SO_LUONG', number_format($value->getDH_SOLUONG(), 0, ",", "."));
                $xtpl->assign('DON_GIA', number_format($value->getHH_DONGIA(), 0, ",", "."));
                $xtpl->assign('THANH_TIEN', number_format(($value->getHH_DONGIA() * $value->getDH_SOLUONG()), 0, ",", "."));
                $xtpl->assign('TRANG_THAI', $value->getDH_TRANGTHAI());
                $xtpl->assign('TK_USERNAME', $value->getTK_USERNAME());
                $xtpl->parse('main.dsdathang');
            }
        } else {
            echo "Chưa có dữ liệu";
        }
        $xtpl->parse('main');
        $xtpl->out('main');
    }

    private function showanh($id) {
        $dir = dirname(__FILE__);
        $xtpl = new XTemplate($dir . DS . "view" . DS . "capnhat.php");
        $anh = $this->loadanh($id);
        if ($anh != null) {
            $xtpl->assign('HINH_ANH', $anh->getHH_HINHANH());
            $xtpl->parse('main.showanh');
        }
        $xtpl->parse('main');
        $xtpl->out('main');
    }

    private function loadAdd() {
        $dir = dirname(__FILE__);
        $xtpl = new XTemplate($dir . DS . "view" . DS . "capnhat.php");
        $xtpl->assign('ACTION', "dathang()");
        $dshanghoa = $this->loadDanhSachHangHoa();
        if ($dshanghoa != null) {
            foreach ($dshanghoa as $value) {
                $xtpl->assign('OPTION_ID_HANGHOA', $value->getHH_ID());
                $xtpl->assign('OPTION_TEN_HANGHOA', $value->getHH_TEN());
                $xtpl->assign('OPTION_HH_GIATIEN', $value->getHH_DONGIA());
                $xtpl->parse('main.optionhanghoa');
            }
        } else {
            echo "chưa có hàng hóa";
        }
        $xtpl->assign('INDEX', INDEX);
        $xtpl->parse('main');
        $xtpl->out('main');
    }

    private function loadEdit($id) {
        $dir = dirname(__FILE__);
        $xtpl = new XTemplate($dir . DS . "view" . DS . "capnhat.php");
        $xtpl->assign('ACTION', "capnhat()");
        $dsdathang = $this->loadanh($id);
        if ($dsdathang != null) {
            $xtpl->assign('HO_TEN', $dsdathang->getDH_HOTEN());
            $xtpl->assign('MSSV', $dsdathang->getDH_MSSV());
            $xtpl->assign('LOP', $dsdathang->getDH_LOP());
            $xtpl->assign('NGAY_SINH', $dsdathang->getDH_NGAYSINH());
            $xtpl->assign('SDT', $dsdathang->getDH_SDT());
            $xtpl->assign('EMAIL', $dsdathang->getDH_EMAIL());
            $xtpl->assign('SO_LUONG', $dsdathang->getDH_SOLUONG());
            $xtpl->assign('NGAY_NHAN', $dsdathang->getDH_NGAYNHAN());
            $xtpl->assign('GHI_CHU', $dsdathang->getDH_GHICHU());
            $xtpl->assign('TEN_CTY', $dsdathang->getDH_TENCTY());
            $xtpl->assign('DIA_CHI_CTY', $dsdathang->getDH_DIACHICTY());
            $xtpl->assign('SDT_CTY', $dsdathang->getDH_SDTCTY());
        } else {
            echo "chưa có hàng hóa";
        }
        $xtpl->assign('INDEX', INDEX);
        $xtpl->parse('main');
        $xtpl->out('main');
    }

    private function loadList() {

        $db = new Database();
        try {
            $dathang = new M_Dat_hang();
            $dathang = $dathang->getList($db);
            return $dathang;
        } catch (Exception $exc) {
            echo "Lỗi: " . $exc->getTraceAsString();
            exit();
        }
    }

    private function loadDanhSachHangHoa() {
        $db = new Database();
        try {
            $hanghoa = new M_Hang_hoa();
            $hanghoa = $hanghoa->getList($db);
            return $hanghoa;
        } catch (Exception $exc) {
            echo "Lỗi: " . $exc->getTraceAsString();
            exit();
        }
    }

    private function loadanh($id) {
        $db = new Database();
        try {
            $dathang = new M_Dat_hang();
            $dathang->setDH_ID($id);
            $kq = $dathang->getByID($db);
            return $kq;
        } catch (Exception $exc) {
            echo "Lỗi: " . $exc->getTraceAsString();
            exit();
        }
    }

    public function dathang($ngaynhan, $soluong, $idhanghoa, $hoten, $mssv, $lop, $ngaysinh, $diachi, $sdt, $email, $ghichu, $taikhoan, $trangthai,$tencty,$diachicty,$sdtcty) {
        $db = new Database();
        try {
            $hanghoa = new M_Dat_hang();
            $hanghoa->setDH_NGAYNHAN($ngaynhan);
            $hanghoa->setDH_SOLUONG($soluong);
            $hanghoa->setHH_ID($idhanghoa);
            $hanghoa->setDH_HOTEN($hoten);
            $hanghoa->setDH_MSSV($mssv);
            $hanghoa->setDH_LOP($lop);
            $hanghoa->setDH_NGAYSINH($ngaysinh);
            $hanghoa->setDH_DIACHI($diachi);
            $hanghoa->setDH_SDT($sdt);
            $hanghoa->setDH_EMAIL($email);
            $hanghoa->setTK_ID($taikhoan);
            $hanghoa->setDH_TRANGTHAI($trangthai);
            $hanghoa->setDH_GHICHU($ghichu);            
            $hanghoa->setDH_TENCTY($tencty);            
            $hanghoa->setDH_DIACHICTY($diachicty);            
            $hanghoa->setDH_SDTCTY($sdtcty);            
            $hanghoa->insert($db);
            $db->submitChanges();
            return 'ok';
        } catch (Exception $exc) {
            echo "Lỗi: " . $exc->getTraceAsString();
            exit();
        }
    }

    private function updatestatus($madathang, $trangthai) {
        $db = new Database();
        try {
            $hanghoa = new M_Dat_hang();
            $hanghoa->setDH_TRANGTHAI($trangthai);
            $hanghoa->setDH_ID($madathang);
            $hanghoa->capnhatTrangthai($db);
            $db->submitChanges();
            return 'ok';
        } catch (Exception $exc) {
            echo "Lỗi: " . $exc->getTraceAsString();
            exit();
        }
    }

    private function delete($madathang) {
        $db = new Database();
        try {
            $hanghoa = new M_Dat_hang();
            $hanghoa->setDH_ID($madathang);
            $hanghoa->delete($db);
            $db->submitChanges();
            return 'ok';
        } catch (Exception $exc) {
            echo "Lỗi: " . $exc->getTraceAsString();
            exit();
        }
    }

    private function update($madathang, $ngaynhan, $soluong, $hanghoa, $hoten, $mssv, $lop, $ngaysinh, $diachi, $sdt, $email, $ghichu) {
        $db = new Database();
        try {
            $hanghoa = new M_Dat_hang();
            $hanghoa->setDH_ID($madathang);
            $hanghoa->setDH_NGAYNHAN($ngaynhan);
            $hanghoa->setDH_SOLUONG($soluong);
            $hanghoa->setHH_ID($hanghoa);
            $hanghoa->setDH_HOTEN($hoten);
            $hanghoa->setDH_MSSV($mssv);
            $hanghoa->setDH_LOP($lop);
            $hanghoa->setDH_NGAYSINH($ngaysinh);
            $hanghoa->setDH_DIACHI($diachi);
            $hanghoa->setDH_SDT($sdt);
            $hanghoa->setDH_EMAIL($email);
            $hanghoa->setDH_GHICHU($ghichu);
            $hanghoa->update($db);
            $db->submitChanges();
            return 'ok';
        } catch (Exception $exc) {
            echo "Lỗi: " . $exc->getTraceAsString();
            exit();
        }
    }

    private function thongke($trangthai) {
        $db = new Database();
        try {
            $hanghoa = new M_Dat_hang();
            return thongke($db, $trangthai);
        } catch (Exception $exc) {
            echo "Lỗi: " . $exc->getTraceAsString();
            exit();
        }
    }

}
